package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.UserPref;
import ve.com.wayoyo.wuml_case.popup.FactoryPopup;
import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

@SuppressLint("ParserError")
public class CompClase extends Componente {

	private Paint Contorno;
	private Paint Texto;
	private Paint Focus;
	
	private float width = 0;
	private float height = 80;
	
	private ArrayList<CompAtributo> Atributos = new ArrayList<CompAtributo>();
	
	
	public CompClase(String Titulo){
		if(UserPref.getInstancia().isUsingConvections() && Titulo.length() > 1){
			this.Titulo = Titulo.substring(0,1).toUpperCase() + Titulo.substring(1);
		}else{
			this.Titulo =  Titulo;
		}
		Contorno = new Paint();
		Contorno.setColor(Color.BLACK);
		Contorno.setStrokeWidth(3);
		Contorno.setAntiAlias(false);
		Texto = new Paint();
		Texto.setColor(Color.BLACK);
		Texto.setTextSize(25);
		Focus = new Paint();
		Focus.setColor(Color.GREEN);
		Focus.setStrokeWidth(4);
		Point punto = new Point();
		punto.x = 20;
		punto.y = 20;
		this.setAnchor(punto);
		width = Texto.measureText(Titulo)+15;
		
		//Solo Para prueba
		addAtributo(new CompAtributo("Nombre","private","String"));
		addAtributo(new CompAtributo("CI","protected","int"));
	}
	
	@Override
	public void Dibujar(Canvas c,boolean isFcoused,float globalScale,int offSetX,int offSetY) {
		int posX = Anchor.x + offSetX;
		int posY = Anchor.y + offSetY;
		Texto.setTextSize(25*globalScale);				
		//Dibujamos el Cuadro
		if(isFocus){
			c.drawLine(posX*globalScale,posY*globalScale,(posX+width)*globalScale,posY*globalScale,Focus);
			c.drawLine(posX*globalScale,posY*globalScale,posX*globalScale,(posY+height)*globalScale,Focus);
			c.drawLine(posX*globalScale,(posY+height)*globalScale,(posX+width)*globalScale,(posY+height)*globalScale,Focus);
			c.drawLine((posX+width)*globalScale,posY*globalScale,(posX+width)*globalScale,(posY+height)*globalScale,Focus);
		
		}else{
			c.drawLine(posX*globalScale,posY*globalScale,(posX+width)*globalScale,posY*globalScale,Contorno);
			c.drawLine(posX*globalScale,posY*globalScale,posX*globalScale,(posY+height)*globalScale,Contorno);
			c.drawLine(posX*globalScale,(posY+height)*globalScale,(posX+width)*globalScale,(posY+height)*globalScale,Contorno);
			c.drawLine((posX+width)*globalScale,posY*globalScale,(posX+width)*globalScale,(posY+height)*globalScale,Contorno);
		}
		
		//Dibujamos la linea del titulo
		c.drawLine(posX*globalScale,(posY+30)*globalScale,(posX+width)*globalScale,(posY+30)*globalScale,Contorno);
		//Dibujamos el titulo
		c.drawText(Titulo, (posX+5)*globalScale ,(posY+25)*globalScale , Texto);
		//Dibujamos los atributos
		int puntoEscritura = 0;
		for (CompAtributo value : Atributos) {
			puntoEscritura += 30;
			c.drawText(value.getText(), (posX+5)*globalScale ,(posY+30+puntoEscritura)*globalScale , Texto);
		}
		//Dibujamos la linea q separa metodos y atributos
		c.drawLine(posX*globalScale,(posY+35+puntoEscritura)*globalScale,(posX+width)*globalScale,(posY+35+puntoEscritura)*globalScale,Contorno);
		//De ser necesario dibujamos el controno que indica el focus
		/*if(isFocus){
			if(globalScale > 1){
				Focus.setStrokeWidth(4);
			}else{
				Focus.setStrokeWidth(2);
			}
			c.drawLine((posX-1)*globalScale,(posY-1)*globalScale,((posX+1)+width)*globalScale,(posY-1)*globalScale,Focus);
			c.drawLine((posX-1)*globalScale,(posY-1)*globalScale,(posX-1)*globalScale,(posY+1+height)*globalScale,Focus);
			c.drawLine((posX-1)*globalScale,(posY+1+height)*globalScale,(posX+1+width)*globalScale,(posY+1+height)*globalScale,Focus);
			c.drawLine((posX+1+width)*globalScale,(posY-1)*globalScale,(posX+1+width)*globalScale,(posY+1+height)*globalScale,Focus);
		}*/
	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,float GlobalScale) {
		boolean Visible = true;
		if((Anchor.x + width) <= offSetX){
			Visible = false;
		}else if(Anchor.x <= offSetX + UserPref.getInstancia().getWinWidth()){
			Visible = false;
		}else if(Anchor.y > offSetY + UserPref.getInstancia().getWinHeight()){
			Visible = false;
		}else if((Anchor.y + height) <= offSetY){
			Visible = false;
		}
		return Visible;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if((touch.x <= Anchor.x+width) && (touch.x >= Anchor.x)){
			if((touch.y <= Anchor.y+height) && (touch.y >= Anchor.y)){
				isFocus = true;
				respuesta = true;
				FactoryPopup.getPopup(VistaDiagrama.getVista(), "Clases");
			}
		}
		return respuesta;
	}

	public void addAtributo(CompAtributo nuevo){
		Atributos.add(nuevo);
		if(width-10 < Texto.measureText(nuevo.getText()) ){
			width = Texto.measureText(nuevo.getText())+10;
		}
		height += 30;
	}

	@Override
	public void setTitulo(String titulo) {
		if(UserPref.getInstancia().isUsingConvections()){
			this.Titulo = titulo.substring(0,1).toUpperCase() + titulo.substring(1);
		}else{
			this.Titulo =  titulo;
		}
	}
	
	

}
